package com.juhuacha.tryrxjava;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.juhuacha.tryrxjava.widget.CheckOverSizeTextView;
import com.juhuacha.tryrxjava.widget.CollapsibleTextView;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

public class DemoAct extends AppCompatActivity {

    ListView mListView;
    List<BaseBean> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        mDatas = new ArrayList<>();
        mListView = (ListView) findViewById(R.id.juhuacha);

        for (int i = 0; i < 20; i++) {
            mDatas.add(new BaseBean(i+"叫撒打算斗奥斯个偶滴哦亲很无敌哦群殴我掉请教我的机器的收费费全额付潜伏期为皮肤好看的舒服哈市的更换皮肤是不过标普根本我个欧虎频繁贵啊不得扫电话覅是的覅偶【哈市地方hi阿斯顿和覅哈市迪欧放还是动画片覅合适的爱上当减肥【片身上的机票是大家发技术都【佛【啊速度金佛【加上东风牌是电话覅哦啊啥活动频繁敷偶的话十多个覅按时的高发", false,true));
        }

        mListView.setAdapter(new TextAdapter());

    }

    private class TextAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mDatas != null ? mDatas.size() : 0;
        }

        @Override
        public Object getItem(int i) {
            return mDatas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View ret = null;
            final int position = i;
  /*          if (view != null) {
                ret = view;
            } else {
                ret = LayoutInflater.from(DemoAct.this).inflate(R.layout.item_tests, viewGroup, false);
            }

            ViewHolder holder = (ViewHolder) ret.getTag();
            if(holder == null){
                holder = new ViewHolder(ret);
                ret.setTag(holder);
            }*/
            ret = LayoutInflater.from(DemoAct.this).inflate(R.layout.item_tests, viewGroup, false);

            CollapsibleTextView text = (CollapsibleTextView) ret.findViewById(R.id.collaps);

            text.setmCallback(new CollapsibleTextView.CollapsStateCallBack() {
                @Override
                public void stateSpread() {
                    mDatas.get(position).setExpand(true);
                    mDatas.get(position).setFirst(false);
                }

                @Override
                public void stateShrinkUp() {
                    mDatas.get(position).setExpand(false);
                    mDatas.get(position).setFirst(false);
                }

                @Override
                public void stateNone() {
                    mDatas.get(position).setFirst(false);
                }

            });
            if ( mDatas.get(i).isFirst()) {

                //holder.text.setFlag(true);
                text.setDesc(mDatas.get(i).getContent(), TextView.BufferType.NORMAL);


               // holder.text.setFlag(true);
            } else {

                if (mDatas.get(i).isExpand()) {
                    //text.setmState(CollapsibleTextView.COLLAPSIBLE_STATE_SPREAD);
                    text.setDesc(mDatas.get(i).getContent(), TextView.BufferType.NORMAL, CollapsibleTextView.COLLAPSIBLE_STATE_SPREAD);
                } else {
                    //text.setmState(CollapsibleTextView.COLLAPSIBLE_STATE_SHRINKUP);
                   text.setDesc(mDatas.get(i).getContent(), TextView.BufferType.NORMAL, CollapsibleTextView.COLLAPSIBLE_STATE_SHRINKUP);
                }


            }

            return ret;
        }
    }

    class ViewHolder {
        CollapsibleTextView text;

        public ViewHolder(View view) {
            text = (CollapsibleTextView) view.findViewById(R.id.collaps);
        }
    }

    class BaseBean {
        private String content;
        private boolean isExpand;
        private boolean isFirst;

        public BaseBean() {
        }

        public BaseBean(String content, boolean isExpand,boolean isFirst) {
            this.content = content;
            this.isExpand = isExpand;
            this.isFirst = isFirst;
        }

        public boolean isFirst() {
            return isFirst;
        }

        public void setFirst(boolean first) {
            isFirst = first;
        }

        public boolean isExpand() {
            return isExpand;
        }

        public void setExpand(boolean expand) {
            isExpand = expand;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }
}
